package cn.iocoder.yudao.module.shop.controller.admin.monthmember;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.monthmember.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.monthmember.ShopMonthMemberDO;
import cn.iocoder.yudao.module.shop.convert.monthmember.ShopMonthMemberConvert;
import cn.iocoder.yudao.module.shop.service.monthmember.ShopMonthMemberService;

@Tag(name = "管理后台 - 顾客每月到店统计")
@RestController
@RequestMapping("/shop/month-member")
@Validated
public class ShopMonthMemberController {

    @Resource
    private ShopMonthMemberService monthMemberService;

    @PostMapping("/create")
    @Operation(summary = "创建顾客每月到店统计")
    @PreAuthorize("@ss.hasPermission('shop:month-member:create')")
    public CommonResult<Long> createMonthMember(@Valid @RequestBody ShopMonthMemberCreateReqVO createReqVO) {
        return success(monthMemberService.createMonthMember(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新顾客每月到店统计")
    @PreAuthorize("@ss.hasPermission('shop:month-member:update')")
    public CommonResult<Boolean> updateMonthMember(@Valid @RequestBody ShopMonthMemberUpdateReqVO updateReqVO) {
        monthMemberService.updateMonthMember(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除顾客每月到店统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:month-member:delete')")
    public CommonResult<Boolean> deleteMonthMember(@RequestParam("id") Long id) {
        monthMemberService.deleteMonthMember(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得顾客每月到店统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:month-member:query')")
    public CommonResult<ShopMonthMemberRespVO> getMonthMember(@RequestParam("id") Long id) {
        ShopMonthMemberDO monthMember = monthMemberService.getMonthMember(id);
        return success(ShopMonthMemberConvert.INSTANCE.convert(monthMember));
    }

    @GetMapping("/list")
    @Operation(summary = "获得顾客每月到店统计列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:month-member:query')")
    public CommonResult<List<ShopMonthMemberRespVO>> getMonthMemberList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopMonthMemberDO> list = monthMemberService.getMonthMemberList(ids);
        return success(ShopMonthMemberConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得顾客每月到店统计分页")
    @PreAuthorize("@ss.hasPermission('shop:month-member:query')")
    public CommonResult<PageResult<ShopMonthMemberRespVO>> getMonthMemberPage(@Valid ShopMonthMemberPageReqVO pageVO) {
        PageResult<ShopMonthMemberDO> pageResult = monthMemberService.getMonthMemberPage(pageVO);
        return success(ShopMonthMemberConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出顾客每月到店统计 Excel")
    @PreAuthorize("@ss.hasPermission('shop:month-member:export')")
    @OperateLog(type = EXPORT)
    public void exportMonthMemberExcel(@Valid ShopMonthMemberExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopMonthMemberDO> list = monthMemberService.getMonthMemberList(exportReqVO);
        // 导出 Excel
        List<ShopMonthMemberExcelVO> datas = ShopMonthMemberConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "顾客每月到店统计.xls", "数据", ShopMonthMemberExcelVO.class, datas);
    }

}
